package com.runvp.rest.biz;

import com.runvp.common.annotation.Log;
import com.runvp.common.annotation.ParamValid;
import com.runvp.common.core.controller.BaseController;
import com.runvp.common.core.domain.AjaxResult;
import com.runvp.common.core.page.TableDataInfo;
import com.runvp.common.enums.BusinessType;
import com.runvp.common.utils.ShiroUtils;
import com.runvp.common.utils.poi.ExcelUtil;
import com.runvp.service.biz.domain.BizCustomerEquity;
import com.runvp.service.biz.service.IBizCustomerEquityService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 顾客权益Controller
 * 
 * @author runvp
 * @date 2022-12-31
 */
@Controller
@RequestMapping("/biz/equity")
public class BizCustomerEquityController extends BaseController {
    private String prefix = "biz/equity";

    @Autowired
    private IBizCustomerEquityService bizCustomerEquityService;

    @RequiresPermissions("biz:equity:view")
    @GetMapping()
    public String equity(ModelMap mmap,Long customerId) {
        mmap.put("customerId", customerId);
        return prefix + "/equity";
    }

    /**
     * 查询顾客权益列表
     */
    @RequiresPermissions("biz:equity:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BizCustomerEquity bizCustomerEquity) {
        startPage();
        List<BizCustomerEquity> list = bizCustomerEquityService.selectBizCustomerEquityList(bizCustomerEquity);
        return getDataTable(list);
    }

    /**
     * 导出顾客权益列表
     */
    @RequiresPermissions("biz:equity:export")
    @Log(title = "顾客权益", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BizCustomerEquity bizCustomerEquity) {
        List<BizCustomerEquity> list = bizCustomerEquityService.selectBizCustomerEquityList(bizCustomerEquity);
        ExcelUtil<BizCustomerEquity> util = new ExcelUtil<BizCustomerEquity>(BizCustomerEquity.class);
        return util.exportExcel(list, "顾客权益数据");
    }

    /**
     * 新增顾客权益
     */
    @GetMapping("/add")
    public String add(ModelMap mmap,Long customerId) {
        mmap.put("customerId", customerId);
        return prefix + "/add";
    }


    /**
     * 生成充电码
     */
    @Log(title = "生成充电码", businessType = BusinessType.OTHER)
    @GetMapping("/create/code")
    @ResponseBody
    public AjaxResult createCode() {
        return AjaxResult.success("生成成功",bizCustomerEquityService.createCode());
    }

    /**
     * 新增保存顾客权益
     */
    @ParamValid
    @RequiresPermissions("biz:equity:add")
    @Log(title = "顾客权益", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BizCustomerEquity bizCustomerEquity) {
        bizCustomerEquity.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(bizCustomerEquityService.insertBizCustomerEquity(bizCustomerEquity));
    }

    /**
     * 修改顾客权益
     */
    @RequiresPermissions("biz:equity:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        BizCustomerEquity bizCustomerEquity = bizCustomerEquityService.selectBizCustomerEquityById(id);
        mmap.put("bizCustomerEquity", bizCustomerEquity);
        return prefix + "/edit";
    }
    /**
     * 顾客权益详情
     */
    @RequiresPermissions("biz:equity:edit")
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap) {
        BizCustomerEquity bizCustomerEquity = bizCustomerEquityService.selectBizCustomerEquityById(id);
        mmap.put("bizCustomerEquity", bizCustomerEquity);
        return prefix + "/detail";
    }
    /**
     * 修改保存顾客权益
     */
    @ParamValid
    @RequiresPermissions("biz:equity:edit")
    @Log(title = "顾客权益", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BizCustomerEquity bizCustomerEquity) {
        bizCustomerEquity.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(bizCustomerEquityService.updateBizCustomerEquity(bizCustomerEquity));
    }

    /**
     * 删除顾客权益
     */
    @RequiresPermissions("biz:equity:remove")
    @Log(title = "顾客权益", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(bizCustomerEquityService.deleteBizCustomerEquityByIds(ids));
    }
}
